/* The file is part of the Waff web framework.
 *
 * Copyright (c) 2011, Grzegorz Skorupa
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditionsare met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list of conditions
 *   and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 *   and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * - Neither the name of the Grzegorz Skorupa nor the names of its contributors may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package pl.org.waff.repository;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.KeyFactory;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;

/**
 *
 * @author greg
 */
public class Comment implements RepositoryObject{
    
    public final static String STATUS_DELETED="deleted";
    public final static String STATUS_HIDDEN="hidden";

    private String id = "new";
    private String parent = "";
    private String title = "";
    private String content = "";
    private String author = "";
    private String email = "";
    private String website = "";
    private Date created;
    private Date modified;
    private String status = "";
    private static final Logger log = Logger.getLogger(Comment.class.getName());

    public Comment(Entity entity) {
        id = KeyFactory.keyToString(entity.getKey());
        parent = (String) entity.getProperty("parent");
        title = (String) entity.getProperty("title");
        content = ((com.google.appengine.api.datastore.Text) entity.getProperty("content")).getValue();
        author = (String) entity.getProperty("author");
        email = (String) entity.getProperty("email");
        website = (String) entity.getProperty("website");
        created = (Date) entity.getProperty("created");
        modified = (Date) entity.getProperty("modified");
        status = (String) entity.getProperty("status");
    }

    public Comment() {
        id = "new";
        parent = "";
        title = "";
        content = "";
        author = "";
        email = "";
        website = "";
        status = "";
    }

    public Entity toEntity() {
        Entity entity;
        if ("new".equals(getId()) || "-1".equals(getId())) {
            entity = new Entity("comment");
        } else {
            entity = new Entity(KeyFactory.stringToKey(getId()));
        }
        entity.setProperty("title", getTitle());
        entity.setProperty("parent", getParent());
        entity.setProperty("author", getAuthor());
        entity.setProperty("email", getEmail());
        entity.setProperty("website", getWebsite());
        entity.setProperty("created", getCreated());
        entity.setProperty("modified", getModified());
        entity.setProperty("content", new com.google.appengine.api.datastore.Text(getContent()));
        entity.setProperty("status", getStatus());
        return entity;
    }

    public void save() {
        if ("new".equals(getId()) || "-1".equals(getId())) {
            setCreated(new Date());
            setModified(getCreated());
        } else {
            setModified(new Date());
        }
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the parent
     */
    public String getParent() {
        return parent;
    }

    /**
     * @param parent the parent to set
     */
    public void setParent(String parent) {
        this.parent = parent;
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @return the content
     */
    public String getContent() {
        return content;
    }

    /**
     * @param content the content to set
     */
    public void setContent(String content) {
        this.content = content;
    }

    /**
     * @return the author
     */
    public String getAuthor() {
        return author;
    }

    /**
     * @param author the author to set
     */
    public void setAuthor(String author) {
        this.author = author;
    }

    /**
     * @return the created
     */
    public Date getCreated() {
        return created;
    }
    
    public Date getPublished(){
        return getCreated();
    }

    /**
     * @param created the created to set
     */
    public void setCreated(Date created) {
        this.created = created;
    }

    /**
     * @return the modified
     */
    public Date getModified() {
        return modified;
    }

    /**
     * @param modified the modified to set
     */
    public void setModified(Date modified) {
        this.modified = modified;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }
    
    public String getCreatedAsString(String dateFormat){
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        return formatter.format(created);
    }
    
    public String getModifiedAsString(String dateFormat){
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        return formatter.format(modified);
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the website
     */
    public String getWebsite() {
        return website;
    }

    /**
     * @param website the website to set
     */
    public void setWebsite(String website) {
        this.website = website;
    }
}
